const wxapi = require("../../utils/wxapi");
// component/phonecode-custom/phonecode-custom.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    entrustInfo: {
      type: Object,
      value: null
    },
    yesOrNo: {
      type: Number,
      value: null
    },
    smsType: {
      type: Number,
      value: null
    },
    phoneNum: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    timeDown: 60,
    timetemp: null,
    isCodeDisabled: false
  },

  /**
   * 组件的方法列表
   */
  methods: {

    /**
     * 获取验证码
     */
    getCode() {
      this.setData({
        isCodeDisabled: true
      })

      if (this.data.smsType === 1 || this.data.smsType === 2 || this.data.smsType === 3) {
        const bodyParams = { smsType: this.data.smsType };
        const headers = {
          'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
        }
        wxapi.sendSms(this.data.phoneNum, bodyParams, headers).then(res => {
          if (res.code === 20000) {
            this.setData({
              isCodeDisabled: true
            })
            this.countDown();
          } else {
            this.setData({
              isCodeDisabled: false
            })
            clearInterval(this.timetemp);
            wx.showModal({
              title: '提示信息',
              content: res.msessage,
              showCancel: false
            })
          }
        })
      } else if (this.data.smsType === 7) {
        const bodyParams = {
          toPhone: this.data.entrustInfo.phoneNum,
          smsType: this.data.smsType,
          paramType: '2',
          stk: this.data.entrustInfo.stk,
        };
        wxapi.sendAuthCodeSms(bodyParams).then(res => {
          if (res.code === 20000) {
            this.setData({
              isCodeDisabled: true
            })
            this.countDown();
          } else {
            this.setData({
              isCodeDisabled: false
            })
            clearInterval(this.timetemp);
            wx.showModal({
              title: '提示信息',
              content: res.msessage,
              showCancel: false
            })
          }
        })
      }
    },

    /**
     * 倒计时
     */
    countDown() {
      this.data.timetemp = setInterval(() => {
        let curTime = this.data.timeDown;
        this.setData({
          timeDown: curTime - 1
        })
        if (this.data.timeDown === 0) {
          clearInterval(this.data.timetemp);
          this.setData({
            timeDown: 60,
            isCodeDisabled: false
          })
        }
      }, 1000);
    },

    /**
     * 
     * @param {*} e 
     */
    submitForm(e) {
      const value = e.detail.value;
      if (!value.smsCode) {
        wx.showToast({
          title: '请输入验证码',
          icon: 'none'
        })
      }
      if (this.data.smsType === 1 || this.data.smsType === 2 || this.data.smsType === 3) {
        this.triggerEvent('checkPhoneCode', { smsCode: value.smsCode });
      } else if (this.data.smsType === 7) {
        this.triggerEvent('checkPhoneCode', { entrustInfo: this.data.entrustInfo, yesOrNo: this.data.yesOrNo, smsCode: value.smsCode });
      }
    },

    /**
     * 取消弹窗
     */
    cancelPopup() {
      clearInterval(this.data.timetemp);
      this.setData({
        timeDown: 60,
        isCodeDisabled: false
      })
      this.triggerEvent('cancelPopup');
    },
  }
})